//
//  PhotoPickerCollectionViewLayout.m
//  PrintFamily
//
//  Created by edaihua on 16/2/28.
//  Copyright © 2016年 ___PrintFactory___. All rights reserved.
//

#import "PhotoPickerCollectionViewLayout.h"

@interface PhotoPickerCollectionViewLayout()

@property (nonatomic, strong) NSMutableDictionary *layoutInfo;

@end

@implementation PhotoPickerCollectionViewLayout

const static NSString * const cellReuseIdentifier   = @"AllPhotosCell";
const static NSString * const headerReuseIdentifier = @"AllPhotosHeader";

- (instancetype)init {
    self = [super init];
    if (self) {
        self.scrollDirection = UICollectionViewScrollDirectionVertical;
        self.headerReferenceSize = CGSizeMake(0.0, 0.0);
        CGSize screenSize = [AppDelegate getScreenSize];
        self.itemSize = CGSizeMake(screenSize.width / 3, screenSize.width / 3);
        self.minimumInteritemSpacing = 0.0f;
        self.minimumLineSpacing = 0.0f;
    }
    return self;
}

- (void)prepareLayout {
    if (_layoutInfo != nil) {
        [_layoutInfo removeAllObjects];
    }
    
    NSMutableDictionary *layoutInfo = [NSMutableDictionary dictionary];
    NSMutableDictionary *cellLayoutInfo = [NSMutableDictionary dictionary];
    NSMutableDictionary *headerLayoutInfo = [NSMutableDictionary dictionary];
    
    CGFloat previousSectionHeight = 0;
    
    for (NSInteger section = 0; section < [self.collectionView numberOfSections]; section++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:section];
        UICollectionViewLayoutAttributes *headerAttribute = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader withIndexPath:indexPath];
        headerAttribute.frame = CGRectMake(0, previousSectionHeight, self.headerReferenceSize.width, self.headerReferenceSize.height);
        
        headerLayoutInfo[indexPath] = headerAttribute;
        
        CGRect previousCellRect = CGRectZero;
        for (NSInteger cell = 0; cell < [self.collectionView numberOfItemsInSection:section]; cell ++) {
            indexPath = [NSIndexPath indexPathForItem:cell inSection:section];
            UICollectionViewLayoutAttributes *cellAttribute = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
            cellAttribute.frame = [self frameForItemAtIndexPath: indexPath withPreviousRect:previousCellRect];
            previousCellRect = cellAttribute.frame;
            cellLayoutInfo[indexPath] = cellAttribute;
        }
        previousSectionHeight = previousCellRect.origin.y + self.itemSize.height;
    }
    
    layoutInfo[headerReuseIdentifier] = headerLayoutInfo;
    layoutInfo[cellReuseIdentifier] = cellLayoutInfo;
    
    self.layoutInfo = layoutInfo;
}

- (CGRect) frameForItemAtIndexPath:(NSIndexPath *)indexPath withPreviousRect:(CGRect)previousRect {
    
    if (CGRectEqualToRect(CGRectZero, previousRect)) {
        return CGRectMake(0.f, self.headerReferenceSize.height, self.itemSize.width, self.itemSize.height);
    } else {
        CGRect cellRect = previousRect;
        cellRect.origin.x = cellRect.origin.x + self.minimumInteritemSpacing + self.itemSize.width;
        if (cellRect.origin.x + self.itemSize.width > self.collectionView.frame.size.width){
            cellRect.origin.x = 0.f;
            cellRect.origin.y = previousRect.origin.y + self.itemSize.height + self.minimumLineSpacing;
        }
        return cellRect;
    }
}

@end
